import presetVar from '../presetVar';
const body1 = window.presetVar.body.body1;
const head1 = window.presetVar.head.head1;

/**
 * 表头合计处理
 */
export function headSumHandle(prop) {
    let busiitemfield = ['orig_amount', 'rest_amount', 'exe_amount', 'org_amount', 'max_amount',
    'org_exe_amount', 'org_rest_amount', 'group_amount', 'group_rest_amount',
    'group_exe_amount', 'global_amount', 'global_rest_amount', 'global_exe_amount'];
    let busiitemfieldTotal = [0, 0, 0, 0, 0,
    0, 0, 0, 0,
    0, 0, 0, 0];
    window.presetVar.bodys.forEach(bodycode => {
        let rows =  prop.cardTable.getAllRows(bodycode, false);
        if(rows && rows[0] && rows.length>0){
            for (let m = 0; m < rows.length; m++) {
                if (rows[m] && rows[m].status != 3) {
                    for (let i = 0; i < busiitemfield.length; i++) {
                        let countmouny = rows[m].values[busiitemfield[i]].value;
                        if (countmouny == null || countmouny == '') {
                            countmouny = 0;
                        }
                        busiitemfieldTotal[i] += +countmouny;
                    }
                }
            }
        }
    })


    for(let i=0;i<busiitemfieldTotal.length;i++){
        let key = busiitemfield[i];
        let changeHeadItem = {
            [busiitemfield[i]]:{ 
                value: busiitemfieldTotal[i],
                display: busiitemfieldTotal[i].toFixed(prop.form.getFormItemsValue(head1,busiitemfield[i]).scale==null?2:prop.form.getFormItemsValue(head1,busiitemfield[i]).scale),
                scale:prop.form.getFormItemsValue(head1,busiitemfield[i]).scale
            }
        }
        prop.form.setFormItemsValue(head1,changeHeadItem);
   }
}


/**
 * 增行、插行时，表体相关字段需根据表头设置其编辑性
 * @param {*} props 
 * @param {*} index 
 */
 export function AddRow(props, index) {
     let rowIndex = props.cardTable.getVisibleRows(body1).length - 1;
     let isEditKeys = ['org_currinfo', 'zzhllx', 'zzhllyrq']; //表体需要控制的编辑性key
     let editkeys = [];
     let unedikeys = [];
     isEditKeys.forEach((item) => {
         props.form.getFormItemsDisabled(head1, item) ? unedikeys.push(item) : editkeys.push(item);
     });
     props.cardTable.setEditableByIndex(body1, rowIndex, editkeys, true);
     props.cardTable.setEditableByIndex(body1, rowIndex, unedikeys, false);
     props.cardTable.setValByKeyAndIndex(
         body1,
         rowIndex,
         'pk_iobsclass',
         props.form.getFormItemsValue(head1, 'pk_iobsclass')
     );
 }

